﻿using Microsoft.AspNetCore.Mvc;
using StackExchange.Redis;

namespace redis_microservice.Controllers
{
    /// <summary>
    /// 项唯一集合 -> 常用于存储用户标签
    /// </summary>
    public class SetController(IConnectionMultiplexer connection) : BaseController(connection)
    {
        /// <summary>
        /// 获取所有项
        /// </summary>
        [HttpGet]
        public async Task<IActionResult> Get(string key = "user:tags")
        { 
            var set = await _db0.SetMembersAsync(key);
            return Ok(set.Select(x => new { Item = x.ToString() }));
        }

        /// <summary>
        /// 添加一项
        /// </summary>
        [HttpPost]
        public async Task<IActionResult> Set(string key = "user:tags", string val = "666")
        {
            bool res = await _db0.SetAddAsync(key, val); // 返回 false 表示值已存在
            return Ok(res);
        }

        /// <summary>
        /// 删除指定项
        /// </summary>
        [HttpPost]
        public async Task<IActionResult> Del(string key = "user:tags", string val = "666")
        {
            bool res = await _db0.SetRemoveAsync(key, val); // 返回 false 表示值不存在
            return Ok(res);
        }

        /// <summary>
        /// 检查指定项是否存在
        /// </summary>
        [HttpPost]
        public async Task<IActionResult> Exists(string key = "user:tags", string val = "666")
        {
            bool res = await _db0.SetContainsAsync(key, val);
            return Ok(res);
        }
    }
}
